(function () {
    $(function () {
        $("header").load("header1.html");
        $("#floor").load("floor.html");
    });
    $(function () {
        let current=JSON.parse(sessionStorage.getItem("userInfo"));
        if(current!=null)
        {
            let data={"u_id":current.u_id}
            $.ajax({
                url:"../../server/getCartsByUser.php",
                data:data
            }).then(function (res) {
                let sum=0;
                // console.log(res);
                for($i=0;$i<res.length;$i++)
                {
                    sum+=parseInt(res[$i].c_num);
                }
                if(sum==0){
                    $(".nullcart").show();
                    $(".shop_table").hide();
                    $(".shop_info").hide();
                }
                else {
                    $(".nullcart").hide();
                    $(".shop_info").show();
                    let str=``;
                    let arr=[];
                    let sum1=0;
                    res.forEach((ele,index)=>{
                        // console.log(ele)
                        let num=parseFloat(ele.c_num*ele.c_nprice).toFixed(2);
                        arr.push(num);
                        sum1=0;
                        for($i=0;$i<arr.length;$i++){
                            sum1+=parseFloat(arr[$i]);
                        }
                        // console.log(sum1);
                        str+=` <tr data-gid=${ele.g_id}>
                    <td width="100" class="bd_R">
                        <a href="details.html?id=${ele.g_id}" target="_blank">
                            <img src=${ele.c_img} alt="" width="70" height="57">
                        </a>
                    </td>
                    <td width="520" class="bd_L align_L">
                        <span class="mar_R">${ele.c_name}</span>
                        <span class="mar_R_red">${ele.c_name1}</span>
                    </td>
                    <td class="shuliang">
                        <div>
                            <a href="javascript:;"> <span class="jian">
                -
           </span></a>

                            <input type="text" class="bianhua" value=${ele.c_num}>
                            <a href="javascript:;"><span class="jia">+</span></a>
                        </div>件
                    </td>
                    <td class="danjia">￥<span class="yige">${ele.c_nprice}</span></td>
                    <td>￥0</td>
                    <td class="money">￥<span class="duoge">${num}</span></td>
                    <td><a href="javascript:;" class="delete">删除</a></td></tr>`
                    });
                    str+=`<tr class="zongji">
                    <td colspan="7" class="align_R">
                            <span class="mar_L">商品数量
                            <a href="" id="total_quantity" class="count">1</a>
                                件
                            </span>
                        <span class="mar_L">商品总价(不含邮费）<a href="" id="total_money">￥${sum1}</a></span>
                    </td>
                </tr>`;
                    // console.log(arr);
                    $("tbody").before(str);
                }
            })
        }
        else {
            $(".nullcart").show();
            $(".shop_table").hide();
            $(".shop_info").hide();
        }

        // console.log(current);
    })
    $(function () {

        $("table").on("click",".jian",function () {
            console.log($($(this).parent().siblings(".bianhua")).val());
            if($(this).parent().siblings(".bianhua").val()<=1){
                $(this).parent().siblings(".bianhua").val(1);
                return;
            }
            let nums=$(this).parent().siblings(".bianhua").val();
            nums-=1;
            $(this).parent().siblings(".bianhua").val(nums);
            let num3=parseInt($(this).parent().siblings(".bianhua").val());
            // console.log($(".bianhua"));
            let count=0;
            let sum=0;
            for($i=0;$i<$(".bianhua").length;$i++)
            {
                console.log($(".duoge")[$i]);
                count+=parseInt($($(".bianhua")[$i]).val())
                sum+=parseInt($($(".duoge")[$i]).text());

            }
            $("#total_money").text("￥"+sum);
            $(".count").text(count);
            $(this).parent().siblings(".bianhua").val(nums);
            let mulit=parseFloat($(this).parent().siblings(".bianhua").val()*parseFloat($(this).parents("tr").find(".yige").text())).toFixed(2);
            $(this).parents("tr").find(".duoge").text(mulit);

            let current=JSON.parse(sessionStorage.getItem("userInfo"));
            let data2={"u_id":current.u_id,"gid":$(this).parents("tr").data("gid"),"num":nums};
            $.ajax({
                url:"../../server/reduce_cart.php",
                data:data2,
                type:"post"
            }).then(function (res) {
                console.log(res);
            })
        })
        $("table").on("click",".jia",function () {
            let self=$(this);
            // console.log($(this).parent().siblings(".bianhua"));
            let nums=parseInt($(this).parent().siblings(".bianhua").val());
            nums+=1;
            $(this).parent().siblings(".bianhua").val(nums);
            let mulit=parseFloat($(this).parent().siblings(".bianhua").val()*parseFloat($(this).parents("tr").find(".yige").text())).toFixed(2);
            $(this).parents("tr").find(".duoge").text(mulit);
            let count=0;
            let sum=0;
            for($i=0;$i<$(".bianhua").length;$i++)
            {
                count+=parseInt($($(".bianhua")[$i]).val())
                sum+=parseFloat($($(".duoge")[$i]).text());

            }
            $("#total_money").text("￥"+sum);
            $(".count").text(count);
            // let num2=$(this).parent().siblings(".bianhua").val();


            // if($(this).parent().siblings(".bianhua").val()>=20){
            //     $(this).parent().siblings(".bianhua").val(20);
            // }
            let current=JSON.parse(sessionStorage.getItem("userInfo"));
            let data1={"u_id":current.u_id,"gid":$(this).parents("tr").data("gid"),"num":nums};
            $.ajax({
                url:"../../server/add_cart.php",
                data:data1,
                type:"post"
            }).then(function (res) {
                console.log(res);
            })
        })
        $("table").on("click",".delete",function () {
            // console.log($(this))
            let current=JSON.parse(sessionStorage.getItem("userInfo"));
            let data2={"u_id":current.u_id,"gid":$(this).parents("tr").data("gid")};
            $.ajax({
                url:"../../server/delete_cart.php",
                data:data2,
                type:"post"
            }).then(function (res) {
                console.log(res);
            });
            alert("您确定要删除吗");
            $($(this).parents("tr")).remove();
            let count=0;
            let sum=0;
            for($i=0;$i<$(".bianhua").length;$i++)
            {
                count+=parseInt($($(".bianhua")[$i]).val())
                sum+=parseInt($($(".duoge")[$i]).text());

            }
            $("#total_money").text("￥"+sum);
            $(".count").text(count);
            if(parseInt($(".count").text())==0){
                $(".nullcart").show();
                $(".shop_table").hide();
                $(".shop_info").hide();
            }

        })
    });
})()